<!--
 * @Description:  输入框 组件
 * @Version: 1.0
 * @Author: Will
 * @Date: 2021-10-15 10:42:56
 * @LastEditors: Will
 * @LastEditTime: 2021-11-11 13:06:33
 * @FilePath: \device-appe:\project\material\components\inputModel\inputModel.vue
 * Copyright (C) 2021 Will. All rights reserved.
-->
<template>
  <div class="inputModel">
    <div class="label">{{ label }}</div>
    <div>
      <input
        class="input"
        :type="type"
        :maxlength="max"
        :placeholder="placeholder"
        v-model="inputVal"
      />
      <div
        v-if="isCode"
        class="code"
        :class="time ? 'code-act' : ''"
        @click="getCode"
      >
        {{ codeTxt }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "inputModel",
  components: {},
  props: {
    title: {
      type: String,
      default: "",
    },
    label: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
    min: {
      type: Number,
      default: 6,
    },
    max: {
      type: Number,
      default: 6,
    },
    err: {
      type: String,
      default: "",
    },
    isCode: {
      type: Boolean,
      default: false,
    },
    placeholder: {
      type: String,
      default: "",
    },
    val: {
      type: String,
      default: "",
    },
    isTime: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      codeTxt: "获取验证码",
      isClick: true,
      time: null,
      inputVal: this.val,
    };
  },
  computed: {},
  watch: {
    inputVal(v) {
      this.$emit("update:val", v);
    },
    isTime(v) {
      if(v){
        this.startTime();
      }
    },
  },
  onLoad(d) {},
  created() {},
  //方法集合
  methods: {
    getCode() {
      if (!this.isClick) {
        this.$util.tip({ title: "请不要重复点击" });
        return;
      }
      this.$emit("getCode", { type: "getCode", val: true });
    },
    startTime() {
      // 点击获取验证码
      this.isClick = false;
      let i = 60;
      this.time = setInterval(() => {
        if (i <= 0) {
          clearInterval(this.time);
          this.time = null;
          this.codeTxt = "获取验证码";
          this.isClick = true;
          this.$emit("getCode", { type: "getCode", val: false });
          return;
        }
        this.codeTxt = i + "s";
        i = i - 1;
      }, 1000);
    },
  },
  onUnload() {
    this.time = null;
    clearInterval(this.time);
  },
};
</script>
<style lang='less' scoped>
.inputModel {
  display: flex;
  flex-direction: row;
  font-size: 34upx;
  color: #1d256e;
  width: 100%;
  border-bottom: 1px solid #ededed;
  padding: 40upx 0 40upx 0upx;
  position: relative;
}
.label {
  width: 200upx;
}
.code {
  color: #1ca9ff;
  font-size: 28upx;
  position: absolute;
  right: 20upx;
  top: 50%;
  transform: translateY(-50%);
  width: 180upx;
  text-align: center;
}
.code-act {
  color: #b6b9c1;
}
.input {
  border: none !important;
  font-size: 34upx;
  color: #1d256e;
}
.input:focus-visible {
  outline: none;
}
</style>